/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.service;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.storage.common.MemberType;
import com.jeesite.modules.storage.dao.VipMemberDao;
import com.jeesite.modules.storage.entity.Member;
import com.jeesite.modules.storage.entity.VipMember;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * VIP会员Service
 */
@Service
@Transactional(readOnly=true)
public class VipMemberService extends CrudService<VipMemberDao, VipMember> {

	@Autowired
	private MemberService memberService;

	/**
	 * 获取单条数据
	 * @param vipMember
	 * @return
	 */
	@Override
	public VipMember get(VipMember vipMember) {
		return super.get(vipMember);
	}
	
	/**
	 * 查询分页数据
	 * @param vipMember 查询条件
	 * @param
	 * @return
	 */
	@Override
	public Page<VipMember> findPage(VipMember vipMember) {
		return super.findPage(vipMember);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param vipMember
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(VipMember vipMember) {
		super.save(vipMember);
		Member filterMember = new Member();
		filterMember.setPhone(vipMember.getPhone());
		Member member = memberService.get(filterMember);
		if(member!=null){
			member.setType(MemberType.VIP_TYPE);
			memberService.save(member);
		}
	}
	
	/**
	 * 更新状态
	 * @param vipMember
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(VipMember vipMember) {
		super.updateStatus(vipMember);
	}
	
	/**
	 * 删除数据
	 * @param vipMember
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(VipMember vipMember) {
		super.delete(vipMember);
	}

	/**
	 * 根据手机号查找VIP会员Id
	 * @param phone 手机号
	 * @return VIP会员Id
	 */
	public String getIdByPhone(String phone){
		VipMember vipMember = new VipMember();
		vipMember.setPhone(phone);
		vipMember = get(vipMember);
		return vipMember == null ? null : vipMember.getId();
	}
}
